package com.xc.stock.domain.basic.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ag.exception.EntityNotFoundException;
import com.ag.utils.AgAssert;
import com.ag.utils.CollectionUtils;
import com.xc.stock.constant.AppVersionType;
import com.xc.stock.constant.AppVersionUpdateTag;
import com.xc.stock.domain.basic.entity.AppVersion;
import com.xc.stock.domain.basic.repository.AppVersionRepository;
import com.xc.stock.infrastructure.db.po.QAppVersionPO;
import com.xc.stock.utils.Cons;
import com.xc.stock.utils.SpringUtils;
import com.xc.stock.utils.Utils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * AppVersionService
 *
 * @author xlfd
 * @email xlfd@gmail.com
 * @created 2021/10/25
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppVersionService {
	final AppVersionRepository repository;
	static QAppVersionPO q = QAppVersionPO.appVersionPO;

	public AppVersion create(AppVersion cmd) {
		repository.save(cmd);
		return cmd;
	}
	
	public AppVersion modify(AppVersion cmd) {
		AppVersion appVersion = repository.find(cmd.getId());
		AgAssert.notNull(appVersion, EntityNotFoundException.class, "找不到信息");
		appVersion.update(cmd);
		repository.save(appVersion);
		return appVersion;
	}
	
	/**
	 * 返回最新的版本
	 * 
	 * @param versionType
	 * @return
	 */
	public AppVersion getLaseter(AppVersionType versionType) {
		List<AppVersion> list = repository.findAll(q.type.eq(versionType.code()), q.createTime.desc());
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}
	
	public AppVersionType dy(String deviceType) {
		AppVersionType versionType = null;

		// 设备是 ios
		if (deviceType.equalsIgnoreCase(Cons.QDeviceType.IOS)) {
			versionType = AppVersionType.USER_APP_IOS;
		}
		// 设备 android
		if (deviceType.equalsIgnoreCase(Cons.QDeviceType.ANDROID)) {
			versionType = AppVersionType.USER_APP_ANDROID;
		}
		
		return versionType;
	}

	/**
	 * 获取用户端版本信息
	 * 
	 * @param request
	 * @param
	 * @return
	 */
	public AppVersion getVersion(String deviceType, String plantForm) {

//		String deviceType = request.getHeader("DeviceType");
//		if (StringUtils.isBlank(deviceType)) {
//			log.error(" Not Set User-Agent ");
//			return null;
//		}

		AppVersionType versionType = dy(deviceType);
		if(versionType == null) {
			return null;
		}
		
		return getLaseter(versionType);
	}

	/**
	 * 获取更新下标志 latest :无需更新 audit : 审核 update_must : 必须更新 update_choose : 强制更新
	 * 
	 * @param request
	 * @param plantForm
	 * @return
	 */
	public AppVersionUpdateTag getUpdateTag(String deviceType, String version, String plantForm) {
		// 获取当前平台 和
		AppVersion appVersion = getVersion(deviceType, plantForm);
		AppVersionUpdateTag tag = AppVersionUpdateTag.LATEST;
		if (null == appVersion) {
			return tag;
		}
		String paramVersion = version;
		if (StringUtils.isBlank(paramVersion)) {
			return tag;
		}
		if (paramVersion.equals(appVersion.getIosAduitVersion())) {
			return AppVersionUpdateTag.AUDIT;
		}
		
		//1.0.1 格式
		if(Utils.highVersion(paramVersion, appVersion.getVersion())) { //高版本
			log.info("高版本: {}, 配置版本: {}", paramVersion, appVersion.getVersion());
			return tag;
		}
		
		if (!paramVersion.equals(appVersion.getVersion())) {
			if (appVersion.mustUpdate()) {
				return AppVersionUpdateTag.UPDATE_MUST;
			} else {
				return AppVersionUpdateTag.UPDATE_CHOOSE;
			}
		}
		return tag;
	}

	public AppVersionRepository repository() {
		return repository;
	}

	public void remove(Integer id) {
		repository.deleteById(id);
	}
	
	public static AppVersionService of() {
		return SpringUtils.getBean(AppVersionService.class);
	}
}
